package com.tmall.tmall.controller.admin;

import com.tmall.tmall.pojo.MainCategory;
import com.tmall.tmall.pojo.PageRt;
import com.tmall.tmall.pojo.ProductRt;
import com.tmall.tmall.pojo.Result;
import com.tmall.tmall.service.MainCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/MainProductType")
public class AdminMainCategoryController {
    @Autowired
    private MainCategoryService mainCategoryService;
    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result Page(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Integer mid){
        PageRt list=mainCategoryService.page(pageNum,pageSize,name);
        return Result.success(list);
    }

    /**
     * 新增分类
     * @param mainCategory
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody MainCategory mainCategory){
        MainCategory cg=mainCategoryService.getByName(mainCategory);
        if(cg!=null){
            return Result.error("分类重复");
        }
        mainCategoryService.insert(mainCategory);
        return Result.success();
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        MainCategory mainCategory = mainCategoryService.getById(id);
        return Result.success(mainCategory);
    }

    /**
     * 修改分类
     * @param mainCategory
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody MainCategory mainCategory){
        mainCategoryService.update(mainCategory);
        return Result.success();
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delBatch")
    public Result deleteByIds(@RequestBody List<Integer> ids){
        mainCategoryService.deleteByIds(ids);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list(){
        List<MainCategory>categories=mainCategoryService.selectAll();
        return Result.success(categories);
    }
}
